package cn.ffcs.unm.majorSkill.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.EnvPath;
import cn.ffcs.unm.majorSkill.action.bean.HighSkilledTalentMainBean;
import cn.ffcs.unm.majorSkill.manager.MajorSkillManager;
import cn.ffcs.unm.majorSkill.model.HighSkilledTalent;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

@Controller
@Scope("prototype")
public class HighSkilledTalentMainComposer extends BasePortletComposer {

	private static final long serialVersionUID = 664736049871429197L;

	private HighSkilledTalentMainBean bean = new HighSkilledTalentMainBean();

	private HighSkilledTalent qryHighSkilledTalent;
	
	@SuppressWarnings("unused")
	private HighSkilledTalent highSkilledTalent;

	@Autowired
	@Qualifier("majorSkillManager") // 业务层注入注解
	private MajorSkillManager majorSkillManager;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}
	/**
	 * 初始化
	 * @throws Exception
	 */
	public void onCreate$highSkilledTalentMainWin() throws Exception {
		//this.onQuery();
	}

	public void onQuery() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		if (!StrUtil.isNullOrEmpty(bean.getMonthId().getValue())) {
			String monthId = formatter.format(bean.getMonthId().getValue());
			clearVariable();
			HighSkilledTalent objHighSkilledTalent = HighSkilledTalent.newInstance();
			PubUtil.fillPoFromBean(bean, objHighSkilledTalent);
			qryHighSkilledTalent = objHighSkilledTalent;
			int index = bean.getRadiogroup().getSelectedIndex();
			if (index == 0) {
				String monthIdMonth = monthId.substring(0, 6);
				qryHighSkilledTalent.setMonthId(monthIdMonth);
			}else {
				qryHighSkilledTalent.setMonthId(monthId);
			}
			this.bean.getHighSkilledTalentListboxPaging().setActivePage(0);
			onHighSkilledTalentQuery();
			highSkilledTalent = null;
		}else{
			ZkUtil.showError("请输入账期", "温馨提示");
		}
		
	}

	public void onReset() {
		bean.getGlbdef1().setValue(null);
		bean.getMonthId().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getHighSkilledTalentListbox());
		this.bean.getHighSkilledTalentListboxPaging().setTotalSize(1);
	}

	public void onHighSkilledTalentQuery() {
		if (this.qryHighSkilledTalent != null) {
			int activePage = bean.getHighSkilledTalentListboxPaging().getActivePage();
			int pageSize = bean.getHighSkilledTalentListboxPaging().getPageSize();
			PageInfo pageInfo = majorSkillManager.queryInfoByHighSkilledTalent(qryHighSkilledTalent,
					activePage + 1, pageSize);
			ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
			bean.getHighSkilledTalentListbox().setModel(dataList);
			bean.getHighSkilledTalentListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}
	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryHighSkilledTalent = null;
		highSkilledTalent = null;
	}
	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.onHighSkilledTalentQuery();
	}
	
	/**
	 * 高技能人才队伍--清单下载
	 * @Description:
	 * @author Wang Yiren
	 * @date 2018年3月12日
	 * @throws Exception
	 * @throws IOException
	 */
	public void onDownload() throws Exception, IOException {
		
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请选择：月表账期", "温馨提示");
			return;
		}
		//日/月表按钮
		int index = bean.getRadiogroup().getSelectedIndex();
		//格式化日期
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		//index == 0 表示月
		if (index == 0) {
			//获取年月--201801
			String monthIdMonth = monthId.substring(0, 6);
			// 获取服务器文件
			String filePath = EnvPath.proPath + monthIdMonth + "_" + "VIEW_DATALIST_HI_PSNDOC_GEF7.csv";
			File file = new File(filePath);
			if (file.exists()) {
				try {
					// 设置字符集
					String charset = "UTF-8";
					// 编码后文件名
					String encodedName = null;
					encodedName = URLEncoder.encode("14高技能人才队伍翻译清单.csv", charset);
					// 将空格替换为+号
					encodedName = encodedName.replace("%20", "+");
					//将文件转化为文件输入流
					FileInputStream in = new FileInputStream(file);
					//掉zk的浏览器下载方法，进行下载
					Filedownload.save(in, "application/octet-stream", encodedName);
				} catch (FileNotFoundException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (IOException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (Exception e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				}
			} else {
				ZkUtil.showError("下载清单失败,请联系管理员", "温馨提示");
			}
		} else {
			ZkUtil.showError("暂不支持日表清单下载", "温馨提示");
		}
	}
	
	/**
	 * 原表清单下载
	 * @Description:
	 * @author Wang Yiren
	 * @date 2018年3月22日
	 * @throws Exception
	 * @throws IOException
	 */
	public void onDownloadNative() throws Exception, IOException {
		
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请选择：月表账期", "温馨提示");
			return;
		}
		//日/月表按钮
		int index = bean.getRadiogroup().getSelectedIndex();
		//格式化日期
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		//index == 0 表示月
		if (index == 0) {
			//获取年月--201801
			String monthIdMonth = monthId.substring(0, 6);
			// 获取服务器文件
			String filePath = EnvPath.proPath + monthIdMonth + "_" + "V_NATIVE_HI_PSNDOC_GLBDEF7.csv";
			File file = new File(filePath);
			if (file.exists()) {
				try {
					// 设置字符集
					String charset = "UTF-8";
					// 编码后文件名
					String encodedName = null;
					encodedName = URLEncoder.encode("14高技能人才队伍原表清单.csv", charset);
					// 将空格替换为+号
					encodedName = encodedName.replace("%20", "+");
					//将文件转化为文件输入流
					FileInputStream in = new FileInputStream(file);
					//掉zk的浏览器下载方法，进行下载
					Filedownload.save(in, "application/octet-stream", encodedName);
				} catch (FileNotFoundException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (IOException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (Exception e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				}
			} else {
				ZkUtil.showError("下载清单失败，请联系管理员", "温馨提示");
			}
		} else {
			ZkUtil.showError("暂不支持日表清单下载", "温馨提示");
		}
	}

}
